package com.jishulianmeng.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSON;
import net.sf.json.JSONObject;
import net.sf.json.util.JSONBuilder;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.struts2.ServletActionContext;


import org.apache.struts2.interceptor.SessionAware;

import com.jishulianmeng.entity.Role;
import com.jishulianmeng.entity.User;
import com.jishulianmeng.service.IGenericService;
import com.jishulianmeng.util.SendEmailImpl;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.sun.accessibility.internal.resources.accessibility;
/**
 * 
 * @author monologu
 *
 */
public class UserCommonAction extends ActionSupport implements SessionAware{

	/**
	 * 
	 */
	private User user = null;
	private Role role = null;
	private IGenericService<User, Serializable> userService = null;
	private IGenericService<Role, Serializable> roleService = null;
	private Map<String,Object> sessionMap;
	/**
	 * 获取session对象
	 */
	@Override
	public void setSession(Map<String, Object> session) {
		this.sessionMap=session;
		
	}
	/**
	 * do login
	 * @return
	 * @author monologu
	 */
	public String doLogin(){
		List<User> users = null;
		String hql = " FROM User u WHERE u.userUsername='" + user.getUserUsername() + "' AND u.userPassword='" + DigestUtils.md5Hex(user.getUserPassword()) + "' ";
		users = userService.searchPage(hql, 0, 1);
		if(null != users && users.size() > 0){
			user = users.get(0);
			//用户账号状态  1表示可以使用  2 账号已停用  
			if(user.getUserStatus()==1)
			{
				sessionMap.put("username", user.getUserUsername());
				long flagRole=userType(user);
				if(flagRole==1){
					sessionMap.put("username", user.getUserUsername());
					return "superManager";
				}else if(flagRole==4)
				{
					sessionMap.put("username", user.getUserUsername());
					return "manager";
				}else if(flagRole==2)
				{
					return SUCCESS;
				}
				
			}else if(user.getUserStatus()==2)
			{
				//提示用户你的账号已经停用
				return "stop";
			}
			return "success";
		} else {
			return "failed";
		}

		
	}
	/**
	 * 判断用户角色
	 * @param user
	 * @return 返回角色id
	 */
	public long userType(User user)
	{
		long userRole= user.getRole().getRoleId();
		return userRole;
	}
	/**
	 * do register
	 * @return
	 * @author monologu
	 */
	public String doRegister(){
		try {
			role = roleService.getObj(2l);
			user.setRole(role);
			user.setUserPassword(DigestUtils.md5Hex(user.getUserPassword()));
			userService.add(user);
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return "failed";
		}
		
	}
	
	/**
	 * 
	 * @return
	 */
	public String checkUsername(){
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response = ServletActionContext.getResponse();
		PrintWriter out = null;
		response.setCharacterEncoding("utf-8");
		
		String username = request.getParameter("username");
		List<User> users = null;
		String hql = " FROM User u WHERE u.name='" + username + "' ";
		users = userService.searchPage(hql, 0, 1);
		
		try {
			out = response.getWriter();
			if(null == users || users.size() < 1){
				out.print("success");
			} else {
				out.print("failed");
			}
			
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			out.flush();
			out.close();
		}
		
		return null;
	}
	
	/**
	 * 
	 * @return
	 */
	public String sendEmailCode(){
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response = ServletActionContext.getResponse();
		PrintWriter out = null;
		response.setCharacterEncoding("utf-8");
		
		String email = request.getParameter("email");
		if(null != email && !"".equals(email)){
			String sendEmailCode = SendEmailImpl.sendEmail(email);
			
			try {
				out = response.getWriter();
				System.out.println(sendEmailCode);
				String string = "{sendEmailCode:'"+DigestUtils.md5Hex(sendEmailCode)+"'}";
				JSONObject jsonObject = JSONObject.fromObject(string);
				out.print(jsonObject.toString());
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				out.flush();
				out.close();
			}
		}

		return null;
	}
	
/*************getter and setter**************/
	
	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public IGenericService<User, Serializable> getUserService() {
		return userService;
	}

	public void setUserService(IGenericService<User, Serializable> userService) {
		this.userService = userService;
	}

	public Role getRole() {
		return role;
	}

	public void setRole(Role role) {
		this.role = role;
	}

	public IGenericService<Role, Serializable> getRoleService() {
		return roleService;
	}

	public void setRoleService(IGenericService<Role, Serializable> roleService) {
		this.roleService = roleService;
	}

	

	
}
